package com.usian.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.usian.admin.mapper.AdChannelMapper;
import com.usian.admin.service.AdChannelService;
import com.usian.model.admin.dtos.ChannelDto;
import com.usian.model.admin.pojos.AdChannel;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
@SuppressWarnings("All")
public class AdChannelServiceImpl implements AdChannelService {
    @Autowired
    private AdChannelMapper adChannelMapper;
    @Override
    public ResponseResult findByNameAndPage(ChannelDto dto) {
//        判断参数 只判断错 不判断对
        //1.参数检测
        if(dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //分页参数检查 进行 page size 的初始化操作
        dto.checkParam();

        //2.安装名称模糊分页查询
        Page page = new Page(dto.getPage(),dto.getSize());
//        拼接条件 构建查询对象
        LambdaQueryWrapper<AdChannel> lambdaQueryWrapper = new LambdaQueryWrapper();
//设置查询条件 1.boolean 为true 执行条件  false 不执行 2.实体类中的属性 3.查询的值
            lambdaQueryWrapper.like(dto.getName()!=null && !"".equals(dto.getName()),AdChannel::getName,dto.getName());
//            根据分页查询方法 page对象 查询对象
        IPage<AdChannel> iPage = adChannelMapper.selectPage(page, lambdaQueryWrapper);

//        使用Java多态方式 向上转型
        ResponseResult responseResult=new PageResponseResult(dto.getPage(),dto.getSize(),(int)iPage.getTotal());
        responseResult.setData(iPage.getRecords());
        return responseResult;
    }
    /*
       添加方法
        */
    @Override
    public ResponseResult save(AdChannel adChannel) {
        //校验参数
        if(adChannel==null){
            //报错
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //添加  要开始考虑 前端能给我们什么数据 我们需要初始化什么数据
        adChannel.setCreatedTime(new Date());
        adChannelMapper.insert(adChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);

    }

    @Override
    public ResponseResult findById(Integer id) {
        if (id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        AdChannel adChannel = adChannelMapper.selectById(id);
        return new ResponseResult().ok(0,adChannel,"查询成功");
    }

    @Override
    public ResponseResult update(AdChannel adChannel) {
        //先判断对象是否为空
        if(adChannel==null){
            //报错
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //先查询数据库是否有对象的对象
        ResponseResult responseResult = this.findById(adChannel.getId());
        if(responseResult.getData()==null){
            //报错
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        adChannelMapper.updateById(adChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult delete(Integer id) {
        if(id==null){
            //报错
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //        先查询对应对象

        ResponseResult responseResult = this.findById(id);
        if (responseResult.getData()==null){
            //报错
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //        根据业务判断 只有状态是 无效的 才可以删除
        AdChannel adChannel=(AdChannel) responseResult.getData();
        if (adChannel.getStatus()){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        adChannelMapper.deleteById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult deletes(Integer[] ids) {
        if(ids==null || ids.length==0){
            //报错
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        adChannelMapper.deleteBatchIds(Arrays.asList(ids));
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);

    }

    @Override
    public ResponseResult findAll() {
//        查询全部  根据状态=true  根据ord字段进行排序 正序
        LambdaQueryWrapper<AdChannel> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AdChannel::getStatus,true);
        lambdaQueryWrapper.orderByAsc(AdChannel::getOrd);
        List<AdChannel> list = adChannelMapper.selectList(lambdaQueryWrapper);
        return ResponseResult.okResult(list);
    }


}
